package cn.com.nes.site.controller.project;

import cn.com.nes.common.em.platform.PlatformEnum;
import cn.com.nes.common.exception.BusinessException;
import cn.com.nes.common.exception.MissRequestParameterException;
import cn.com.nes.common.util.XzqhUtils;
import cn.com.nes.common.web.CommonResult;
import cn.com.nes.site.controller.project.vo.*;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.em.project.ProjectServiceUserTypeEnum;
import cn.com.nes.site.service.project.ProjectServiceUserAuthService;
import cn.com.nes.site.service.project.bo.QRCodeForSaveServiceUserBO;
import cn.com.nes.site.service.project.dto.ProjectServiceUserDTO;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = {"项目模块-服务人员管理"})
@Controller
@RequestMapping("/v2/project/service/user")
public class ProjectServiceUserController {

    @Resource
    ProjectServiceUserAuthService projectServiceUserAuthService;

    /**
     * 查询项目服务人员分页列表
     * @return
     */
    @Deprecated
    @RequestMapping(value = "/getProjectServiceUserPage", method = RequestMethod.GET)
    public @ResponseBody CommonResult<ProjectServiceUserDTO> getProjectServiceUserPage(ProjectServiceUserPageVO vo) throws Exception {
        return CommonResult.page(projectServiceUserAuthService.getProjectServiceUserPage(vo.convertToBo()));
    }

    @ApiOperation(value = "项目服务人员权限分页列表", notes = "项目设置施工队伍时，查询项目服务人员权限分页列表")
    @RequestMapping(value = "/getProjectServiceUserAuthPage", method = RequestMethod.GET)
    public @ResponseBody CommonResult<ProjectServiceUserDTO> getProjectServiceUserAuthPage(
            @Validated ProjectServiceUserPageVO vo) throws Exception {
        return CommonResult.page(projectServiceUserAuthService.getProjectServiceUserAuthPage(vo.convertToBo()));
    }

    @ApiOperation(value = "项目服务人员保存", notes = "项目设置施工队伍时，服务人员权限保存")
    @RequestMapping(value = "/saveProjectServiceUser", method = RequestMethod.POST)
    public @ResponseBody PageData saveProjectServiceUser(
            @RequestBody @Validated SaveProjectServiceUserVO vo) throws Exception {
        if (CollectionUtil.isEmpty(vo.getAreaCodeList())) {
            throw new MissRequestParameterException("请设定该施工人员服务范围");
        }
        return projectServiceUserAuthService.saveProjectServiceUser(vo.convertToBo());
    }

    @ApiOperation(value = "项目服务人员启停用", notes = "项目设置施工队伍时，服务人员启用、停用标识设置")
    @RequestMapping(value = "/enableProjectServiceUser", method = RequestMethod.POST)
    public @ResponseBody CommonResult<?> enableProjectServiceUser(
            @RequestBody @Validated EnableProjectServiceUserVO vo) throws Exception {
        projectServiceUserAuthService.enableProjectServiceUserAuth(vo.convertToBo());
        return CommonResult.ok("操作成功");
    }

    @ApiOperation(value = "项目服务人员设置服务区域", notes = "项目设置施工队伍时，服务人员设置服务区域。易修APP调用（仅施工人员）")
    @RequestMapping(value = "/setProjectServiceUserAreaCode", method = RequestMethod.POST)
    public @ResponseBody CommonResult<?>  setProjectServiceUserAreaCode(
            @RequestBody @Validated SetProjectServiceUserAreaCodeVO vo) throws Exception {
/*        if (vo.getAreaCodeList().stream().anyMatch(xzqh -> 5 > XzqhUtils.getRegionLevel(xzqh))) {
            throw new BusinessException("施工人员的服务范围只能选择镇级以下的行政区划！");
        }*/
        // 开发团队认为只允许施工人员选择村级行政区划对操作不友好,先放大到镇级
        if (vo.getAreaCodeList().stream().anyMatch(xzqh -> 4 > XzqhUtils.getRegionLevel(xzqh))) {
            throw new BusinessException("施工人员的服务范围只能选择镇级或村级的行政区划！");
        }
        projectServiceUserAuthService.setProjectServiceUserAreaCode(vo.convertToBo());
        return CommonResult.ok("操作成功");
    }

    @ApiOperation(value = "项目服务人员删除", notes = "项目设置施工队伍时，服务人员删除")
    @RequestMapping(value = "/delProjectServiceUser", method = RequestMethod.POST)
    public @ResponseBody PageData delProjectServiceUser(
            @RequestBody @Validated DelProjectServiceUserVO vo) throws Exception {
        return projectServiceUserAuthService.delProjectServiceUser(vo);
    }

    /**
     * 查询项目服务负责人列表 - 不分页
     */
    @Deprecated
    @RequestMapping(value = "/getConstructionSupervisorList", method = RequestMethod.GET)
    public @ResponseBody PageData getConstructionSupervisorList(GetConstructionSupervisorListVO vo) throws Exception {
        return PageData.ok("constructionSupervisorList", projectServiceUserAuthService.getConstructionSupervisorList(vo));
    }

    /**
     * 查询我的施工队列表
     */
    @Deprecated
    @RequestMapping(value = "/getMyTeamInfoList", method = RequestMethod.GET)
    public @ResponseBody PageData getMyTeamInfoList(GetMyTeamInfoVO vo) throws Exception {
        return PageData.ok("getMyTeamInfoList", projectServiceUserAuthService.getMyTeamInfoList(vo));
    }

    /**
     * 我的施工队的项目列表
     */
    @RequestMapping(value = "/getMyTeamProjectList", method = RequestMethod.GET)
    public @ResponseBody PageData getMyTeamProjectList(GetMyTeamProjectVO vo) throws Exception {
        return PageData.ok("myTeamProjectList", projectServiceUserAuthService.getMyTeamProjectList(vo));
    }

    @ApiOperation("我的施工队伍中的服务人员列表")
    @RequestMapping(value = "/getMyTeamMemberList", method = RequestMethod.GET)
    public @ResponseBody PageData getMyTeamMemberList(@Validated GetMyTeamMemberVO vo) throws Exception {
        return PageData.ok("myTeamMemberList", projectServiceUserAuthService.getMyTeamMemberList(vo, false));
    }

    @ApiOperation("我的施工队负责的区域信息")
    @RequestMapping(value = "/getMyTeamWorkArea", method = RequestMethod.GET)
    public @ResponseBody PageData getMyTeamWorkArea(GetMyTeamWorkAreaVO vo) throws Exception {
        return PageData.ok("myTeamWorkAreaList", projectServiceUserAuthService.getMyTeamWorkArea(vo));
    }

    @ApiOperation(value = "获取服务人员二维码信息", notes = "根据二维码类型，获取对应的二维码信息。 0-施工人员的二维码，1-施工负责人的二维码。 热链云PC生成施工负责人的二维码，易修APP生成施工人员的二维码", tags = {"易修APP"})
    @RequestMapping(value = "/getQRCodeInfo", method = RequestMethod.GET)
    public @ResponseBody PageData getQRCodeInfo(@Validated GetProjectQRCodeInfoVO vo) throws Exception {
        if (ProjectServiceUserTypeEnum.CONSTRUCTION_MANAGER.getKey().equals(vo.getQrCodeType())) {
            if (StrUtil.isEmpty(vo.getDepartmentId())) {
                throw new MissRequestParameterException("部门");
            }
        }
        if (PlatformEnum._easy_repair.getCode().equals(vo.getOperatePlatform())) {
            if (!projectServiceUserAuthService.validateHeadAuth(vo.getOperateUserId())) {
                throw new BusinessException("当前用户没有操作权限！");
            }
        }
        return PageData.ok("qrCodeInfo", projectServiceUserAuthService.getQRCodeInfo(vo));
    }

    @ApiOperation(value = "扫码保存项目服务人员", notes = "扫描人员二维码，保存、绑定项目服务人员", tags = {"易修APP"})
    @RequestMapping(value = "/qRCodeForSaveProjectServiceUser", method = RequestMethod.POST)
    public @ResponseBody PageData qRCodeForSaveProjectServiceUser(
            @RequestBody @Validated QRCodeForSaveProjectServiceUserVO vo) throws Exception {
        if (ProjectServiceUserTypeEnum.CONSTRUCTION_MANAGER.getKey().equals(vo.getServiceUserType())) {
            if (StrUtil.isEmpty(vo.getDepartmentId())) {
                throw new MissRequestParameterException("部门");
            }
        }
        QRCodeForSaveServiceUserBO bo = vo.convertToBO();
        bo.setErUserId(vo.getOperateUserId());
        bo.setUsername(vo.getOperateUserName());
        bo.setPhone(vo.getOperateUserPhone());
        bo.setNickname(vo.getOperateNickName());
        return projectServiceUserAuthService.qRCodeForSaveProjectServiceUser(bo);
    }

    @ApiOperation(value = "校验是否为施工负责人", notes = "校验当前登录人是否为施工负责人， 易修APP调用", tags = {"易修APP"})
    @RequestMapping(value = "/validHeadServiceUser", method = RequestMethod.GET)
    public @ResponseBody PageData validHeadServiceUser(@RequestParam Long easyRepairUserId) throws Exception {
        return PageData.ok("isForeman", projectServiceUserAuthService.validateHeadAuth(easyRepairUserId));
    }

}
